/**
 * 
 */
package br.com.rafael.gestorinvestimentos.util;

import javax.swing.JOptionPane;

/**
 * @author Rafael Sperendio
 *
 */
public final class OptionPaneUtils {
	
	private OptionPaneUtils() {
	}
	public static int getSelection(JOptionPane optionPane) {
		// Default return value, signals nothing selected
		int returnValue = JOptionPane.CLOSED_OPTION;
		// Get selected value
		Object selectedValue = optionPane.getValue();
		// If none, then nothing selected
		if (selectedValue != null) {
			Object options[] = optionPane.getOptions();
			if (options == null) {
			// Default buttons, no array specified
				if(selectedValue instanceof Integer) {
					returnValue = ((Integer)selectedValue).intValue();
				}
			} else {
				// Array of option buttons specified
				for (int i=0, n = options.length; i < n; i++) {
					if(options[i].equals(selectedValue)) {
						returnValue = i;
						break; // out of for loop
					}
				}
			}
		}
		return returnValue;
	}
	
	public static JOptionPane getNarrowOptionPane(int maxCharactersPerLineCount) {
		// Our inner class definition
		class NarrowOptionPane extends JOptionPane {
			int maxCharactersPerLineCount;
			
			NarrowOptionPane(int maxCharactersPerLineCount) {
				this.maxCharactersPerLineCount = maxCharactersPerLineCount;
			}
			
			public int getMaxCharactersPerLineCount() {
				return maxCharactersPerLineCount;
			}
		}
		return new NarrowOptionPane(maxCharactersPerLineCount);
	}
}
